﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GtsServer.Model;
using GtsServer.DAL;
using System.Data;
using GtsServer.Common;

namespace GtsServer.Manage.Controller
{
    public class TaskController
    {
        StartEndTaskService.ServiceClient client = new StartEndTaskService.ServiceClient();
        TaskDAL dal = new TaskDAL();
        public List<Task> GetList(int typeID, int pageIndex, int pageSize, out int count)
        {
            count = 0;
            DataSet ds = typeID == 0 ? dal.GetList(string.Empty, pageIndex, pageSize) : dal.GetListByTypeID(typeID, pageIndex, pageSize);
            count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            return IListDataSet.DataSetToIList<Task>(ds, 0).ToList();
        }

        /// <summary>
        /// 获取比赛名称  比赛人员管理
        /// </summary>
        /// <param name="typeID"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public List<Task> GetList(int typeID)
        {
            DataSet ds = dal.GetList(typeID);
            
            return IListDataSet.DataSetToIList<Task>(ds, 0).ToList();
        }

        /// <summary>
        /// 比赛公告调用 
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Task> GetListByTaskTypeID(int taskTypeID, string TaskName, int pageIndex, int pageSize, out int count)
        {
            count = 0;
            List<Task> list = new List<Task>();
            string strWhere = string.Empty;
            if (taskTypeID > 0)
                strWhere += " and TaskTypeID=" + taskTypeID;
            if (!string.IsNullOrEmpty(TaskName))
                strWhere += " and Name like '%" + TaskName + "%'";
            DataSet ds = dal.GetListByTaskTypeID(pageIndex, pageSize, strWhere);
            count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
            list = IListDataSet.DataSetToIList<Task>(ds, 0).ToList();
            return list;
        }
        public List<Task> GetListByPrize()
        {
            List<Task> list = new List<Task>();
            DataSet ds = dal.GetListByPrize();
            list = IListDataSet.DataSetToIList<Task>(ds, 0).ToList();
            return list;
        }

        public int Add(Task model)
        {
            int id = dal.Add(model);
            model.ID = id;
            StartEndTaskService.ServiceClient client = new StartEndTaskService.ServiceClient();
            client.Edit(model);
            return id;
        }

        public bool Update(Task model)
        {
            bool isSuccess = dal.Update(model);
            if (isSuccess)
            {
                StartEndTaskService.ServiceClient client = new StartEndTaskService.ServiceClient();
                client.Edit(model);
            }
            return isSuccess;
        }

        public bool AddArrayTask(int taskID, int taskNum)
        {
            try
            {
                if (taskNum > 0)
                {
                    List<Task> list = new List<Task>();
                    DataSet ds = dal.AddArrayTask(taskID, taskNum);
                    list = IListDataSet.DataSetToIList<Task>(ds, 0).ToList();
                    foreach (var item in list)
                    {
                        StartEndTaskService.ServiceClient client = new StartEndTaskService.ServiceClient();
                        client.Edit(item);
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(ex);
                return false;
            }

        }

        public bool Delete(int id)
        {
            bool isSuccess = dal.Delete(id);
            if (isSuccess)
            {
                StartEndTaskService.ServiceClient client = new StartEndTaskService.ServiceClient();
                client.Delete(id);
            }
            return isSuccess;
        }
    }
}
